package com.project;

import com.project.servlet.JSONServlet;
import com.project.servlet.TestServlet;
import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Wrapper;
import org.apache.catalina.startup.Tomcat;

/**
 * 启动类
 */
public class MainWeb1 {
    public MainWeb1() {
        Tomcat tomcat = new Tomcat();
        //设置tomcat启动端口
        tomcat.setPort(8088);
        //引导http引擎
        tomcat.getConnector();

        //常见上下文
        Context context = tomcat.addContext("",null);
        try {
            //注册servlet
            Wrapper w = Tomcat.addServlet(context,"testServlet",new TestServlet());
            //该值为正数或0时，表示容器一启动，完成该servlet的初始化动作
            //该值为负数或默认时，表示第一次访问该servlet完成初始化动作
            w.setLoadOnStartup(1);
            //设置客户端请求的URL路径
            w.addMapping("/user/*");

            Wrapper wjson = Tomcat.addServlet(context,"json",new JSONServlet());
            wjson.addMapping("/json");

            //启动tomcat
            tomcat.start();
        } catch (LifecycleException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        new MainWeb1();
    }
}
